package cn.com.platform.web.manage.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.easy.exception.BusinessException;
import cn.com.easy.permission.model.UserModel;
import cn.com.easy.permission.service.system.urlreserve.anotion.UrlReserveAnotion;
import cn.com.easy.permission.web.controller.BaseController;
import cn.com.easy.permission.web.dto.MessageDTO;
import cn.com.easy.permission.web.dto.PageRequestParamDTO;
import cn.com.easy.utils.Page;
import cn.com.easy.utils.PageUtils;
import cn.com.easy.utils.ResponseOutputUtils;
import cn.com.platform.dao.ArticleCommentDao;
import cn.com.platform.entity.ArticleCommentEntity;
import cn.com.platform.mybatis.ArticleCommentMybatisDao;
import cn.com.platform.web.manage.dto.ArticleCommentDto;
import cn.com.platform.web.manage.dto.ArticleCommentQueryParamDto;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * 文章评论管理
 * 
 * @author wxy 2016年8月3日
 * 
 */
@Controller
@RequestMapping("/manage/article/comment")
public class ArticleCommentManageController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(ArticleCommentManageController.class);

	/** 文章评论dao */
	@Autowired
	private ArticleCommentDao articleCommentDao;
	/** 文章评论MybatisDao */
	@Autowired
	private ArticleCommentMybatisDao articleCommentMibatisDao;

	/**
	 * 文章评论管理
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @author wxy 2016年8月3日
	 */
	/*@UrlReserveAnotion("文章评论管理")
	@RequestMapping("")
	public String index(HttpServletRequest request, HttpServletResponse response) {
		return "/manage/article/article_comment";
	}*/

	/**
	 * 获取所有文章评论
	 * 
	 * @param request
	 * @param response
	 * @param pageRequestParamDTO
	 * @author wxy 2016年8月3日
	 */
	@UrlReserveAnotion("获取所有文章评论")
	@RequestMapping("/list")
	public void getAllComment(HttpServletRequest request, HttpServletResponse response, PageRequestParamDTO paramDto, final ArticleCommentQueryParamDto param) {
		try {
			if (paramDto.getSortOrder() == null) {
				paramDto.setSortName("id");
				paramDto.setSortOrder("desc");
			}
			PageList<ArticleCommentDto> pageObject = articleCommentMibatisDao.queryArticleComments(param, paramDto.buildMyBatisPageRequest());
			Page<ArticleCommentDto> page = PageUtils.getPage(pageObject);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, page));
		} catch (Exception ex) {
			logger.error("获取所有文章评论异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "获取所有文章分类异常.ex:" + ex.getLocalizedMessage()));
		}
	}

	/**
	 * 保存或更新文章评论
	 * 
	 * @param request
	 * @param response
	 * @param roleModel
	 * @author wxy 2016年8月10日
	 *//*
	@UrlReserveAnotion("保存或更新评论")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void saveOrUpdateArticle(HttpServletRequest request, HttpServletResponse response, ArticleCommentDto articleCommentDto) {
		try {
			ArticleEntity articleEntity = articleDao.findOne(articleCommentDto.getArticleId());
			if (articleEntity == null) {
				throw new BusinessException("文章id不存在");
			}
			MemberEntity memberEntity = memberDao.findOne(articleCommentDto.getUid());
			if (memberEntity == null) {
				throw new BusinessException("用户id不存在");
			}
			if (articleCommentDto.getId() == null) {
				// 新增
				ArticleCommentEntity articleCommentEntity = new ArticleCommentEntity();
				BeanUtils.copyProperties(articleCommentDto, articleCommentEntity);
				articleCommentDao.save(articleCommentEntity);
			} else {
				// 更新
				ArticleCommentEntity articleCommentTemp = articleCommentDao.findOne(articleCommentDto.getId());

				if (articleCommentTemp == null) {
					throw new BusinessException("未找到该文章评论！");
				} else {
					articleCommentTemp.setArticleId(articleCommentDto.getArticleId());
					articleCommentTemp.setUid(articleCommentDto.getUid());
					articleCommentTemp.setContent(articleCommentDto.getContent());
					articleCommentTemp.setClosed(articleCommentDto.getClosed());
					articleCommentDao.save(articleCommentDto);
				}
			}
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, ""));
		} catch (Exception ex) {
			logger.error("提交文章评论信息异常.", ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "提交文章评论信息异常.ex:" + ex.getLocalizedMessage()));
		}
	}*/

	/**
	 * 删除文章评论
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author wxy 2016年8月10日
	 */
	@UrlReserveAnotion("删除文章评论")
	@RequestMapping(value = "/delete")
	public void deleteArticle(HttpServletRequest request, HttpServletResponse response, ArticleCommentEntity articleCommentEntity) {
		try {
			UserModel user = getCurrentUser(request);
			if (user == null) {
				throw new BusinessException("用户不存在，请重新登录");
			}
			if(articleCommentEntity.getId()==null){
				throw new BusinessException("未找到该评论");
			}
			ArticleCommentEntity articleCommentEntityTemp = articleCommentDao.findByIdAndContent(articleCommentEntity.getId(), articleCommentEntity.getContent());
			if (articleCommentEntityTemp == null) {
				throw new BusinessException("未找到该评论");
			}
			// 删除文章评论
			articleCommentEntityTemp.setDeleteStatus(true);
			articleCommentEntityTemp.setLastModifyBy(user.getLoginname());
			articleCommentEntityTemp.setLastModifyTime(new Date());
			articleCommentDao.save(articleCommentEntityTemp);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, "删除文章评论成功"));
		} catch (Exception ex) {
			logger.error("删除文章评论异常，id:" + articleCommentEntity.getId(), ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "删除文章评论异常.ex:" + ex.getLocalizedMessage()));
		}
	}
	

	/**
	 * 关闭/开启文章评论
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author wxy 2016年8月10日
	 */
	@UrlReserveAnotion("关闭/开启文章评论")
	@RequestMapping(value = "/close")
	public void closeArticle(HttpServletRequest request, HttpServletResponse response, ArticleCommentEntity articleCommentEntity) {
		try {
			UserModel user = getCurrentUser(request);
			if (user == null) {
				throw new BusinessException("用户不存在，请重新登录");
			}
			if(articleCommentEntity.getId()==null){
				throw new BusinessException("未找到该评论");
			}
			ArticleCommentEntity articleCommentEntityTemp = articleCommentDao.findByIdAndContent(articleCommentEntity.getId(), articleCommentEntity.getContent());
			if (articleCommentEntityTemp == null) {
				throw new BusinessException("未找到该评论");
			}
			// 关闭/开启文章评论
			if(articleCommentEntityTemp.getClosed()==true){
				articleCommentEntityTemp.setClosed(false);
			}else{
				articleCommentEntityTemp.setClosed(true);
			}
			articleCommentEntityTemp.setLastModifyBy(user.getLoginname());
			articleCommentEntityTemp.setLastModifyTime(new Date());
			articleCommentDao.save(articleCommentEntityTemp);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", true, "关闭文章评论成功"));
		} catch (Exception ex) {
			logger.error("关闭文章评论异常，id:" + articleCommentEntity.getId(), ex);
			ResponseOutputUtils.renderJson(response, MessageDTO.newInstance("", false, "关闭文章评论异常.ex:" + ex.getLocalizedMessage()));
		}
	}
}
